import { createRouter, createWebHistory } from 'vue-router'
import PkIndexView from '../views/pk/PkIndexView'
import RecordIndexView from '../views/record/RecordIndexView'
import RecordContentView from '../views/record/RecordContentView'
import RanklistIndexView from '../views/ranklist/RanklistIndexView'
import UserBotIndexView from '../views/user/bot/UserBotIndexView'
import NotFound from '../views/error/NotFound'
import UserAccountLoginView from '../views/user/account/UserAccountLoginView';
import UserAccountRegisterView from '../views/user/account/UserAccountRegisterView';
import store from '@/store'
const routes = [
  {
    path:'/',
    redirect:'/pk/',
    name:'home',
    meta:{
      requestAuth: true,//主页需要授权
    }
   
 },
    {
       path:'/pk/',
       component:PkIndexView,
       name:'pk_index',
       meta:{
        requestAuth: true,//主页需要授权
      }
    },

    {
      path:'/record/',
      component:RecordIndexView,
      name:'record_index',
      meta:{
        requestAuth: true,//主页需要授权
      }
   },

    {
      path:'/record/:recordId/',
      component:RecordContentView,
      name:'record_content',
      meta:{
        requestAuth: true,//主页需要授权
      }
    },


      {
        path:'/ranklist/',
        component:RanklistIndexView,
        name:'ranklist_index',
        meta:{
          requestAuth: true,//主页需要授权
        }
      },

      {
        path:'/user/bot/',
        component:UserBotIndexView,
        name:'user_bot_index',
        meta:{
          requestAuth: true,//主页需要授权
        }
      },

      {
        path:'/404/',
        component:NotFound,
        name:'404',
        meta:{
          requestAuth: false,//主页需要授权
        }
      },

      {
        path:'/user/account/login/',
        component:UserAccountLoginView,
        name:'user_account_login',
        meta:{
          requestAuth: false,//主页需要授权
        }
      },
      
      {
        path:'/user/account/register/',
        component:UserAccountRegisterView,
        name:'user_account_register',
        meta:{
          requestAuth: false,//主页需要授权
        }
      },
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

router.beforeEach((to,from,next) => {
  if(to.meta.requestAuth && !store.state.user.is_login){
     next({name:"user_account_login"});
  }
  else{
    next();
  }
})

export default router